import { callbackData } from "../utils/data.handler";
import { access, sql } from "../logger";
import validate from "../utils/validator";
import { Context, Next } from "koa";
import { Rules } from "async-validator";

//创建部门信息基本规则校验
export const baseDepartValidator = async (ctx:Context,next:Next)=>{
    const rules: Rules = {
        name: {
            type: 'string',
            required: true,
            message: '部门名称不能为空'
        },
        parent_id: {
            type: 'number',
            required: true,
            message: '父级部门不能为空'
        },
        code: {
            type: 'string',
            required: true,
            message: '部门编码不能为空'
        }
    }
    const { error } = await validate<any>(ctx, rules);
    if(error){
        access.info('部门信息校验错误',{msg:error})
        console.error('部门信息校验错误',{msg:error})
        ctx.app.emit('error',callbackData('1',error),ctx);
        return false;
    }
    await next();
}